package com.czk.controller;

import com.czk.entity.PageResult;
import com.czk.entity.Result;
import com.czk.entity.StatusCode;
import com.czk.anotation.OperateLog;
import com.czk.model.TbSpu;
import com.czk.service.SpuService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/20 16:23
 */
@RestController
@RequestMapping("/spu")
public class SpuController {
    @Resource
    private SpuService spuService;

    @GetMapping("/{spuId}")
    public Result<TbSpu> findById(@PathVariable("spuId") String spuId) {
        TbSpu tbSpu = spuService.findById(spuId);
        return new Result<TbSpu>(true, StatusCode.OK, "查询成功", tbSpu);
    }

    @PostMapping("/search/{page}/{size}")
    @OperateLog
    public Result findPage(@RequestBody Map<String, String> searchMap, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page pagResult = spuService.search(searchMap, page, size);
        long total = pagResult.getTotal();
        List result = pagResult.getResult();
        PageResult pageResult = new PageResult(total, result);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}
